//
//  LWPhotoSelectViewController.m
//  1.28  weibo
//
//  Created by mac on 15/2/13.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "LWPhotoSelectViewController.h"
#import "LWPhotoSelectCell.h"

@interface LWPhotoSelectViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@end

@implementation LWPhotoSelectViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
   //1.监听点击事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addPhotos) name:LWAddPhotoNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteClick:) name:LWDeletePhotoNotification object:nil];
}
- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)addPhotos{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}
- (void)deleteClick:(NSNotification *)note{

    //1.取出要删除的图片
    LWPhotoSelectCell *cell = note.object;
    //2.删除要删除的图片
    [self.images removeObjectAtIndex:cell.indexPath.item];
    //3.刷新表格
    [self.collectionView reloadData];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    //1.取出选中的图片
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    //2.保存图片
    [self.images addObject:image];
    //3.刷新表格
    [self.collectionView reloadData];
    //4.关闭图片选择器
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 初始化每个按钮的相关属性
- (void)viewWillLayoutSubviews{

    // 设置水平间隙
    self.layout.minimumInteritemSpacing = 10;
    // 设置垂直间隙
    self.layout.minimumLineSpacing = 10;
    // 设置全局左右间隙
    self.layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    // 2.设置宽高
    // 列数
    CGFloat col = 3;
    // 计算
    CGFloat width = (self.view.width - ((col + 1) * 10)) / 3.0;
    CGFloat height = width;
    // 设置全局item的size
    self.layout.itemSize = CGSizeMake(width, height);
    
}

#pragma mark - ControllerView的代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.images.count + 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    LWPhotoSelectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photoCell" forIndexPath:indexPath];
    if (self.images.count == indexPath.item ) {
        cell.iconImage = nil;
    }else{
    
        cell.indexPath = indexPath;
        cell.iconImage = self.images[indexPath.item];
    }
    return cell;
}

#pragma mark - 懒加载
- (NSMutableArray *)images{
    if (_images == nil) {
        _images = [NSMutableArray array];
    }
    return _images;
}

@end
